<template>
  <div class="login-box">
    <div style="width: 50%; height: 100%">
      <img
        style="width: 100%; height: 100%"
        src="../../assets/404_images/404_cloud.png"
        alt=""
      />
    </div>
    <div class="login-container">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
      <div v-if="formItem1">
         <div class="title-container">
          <h3 :class="{active: elform === index}" class="title" v-for="(item,index) in list" :key="index" @click="elform = index">{{item}}</h3>
        </div>

        <!-- 账号登录 -->
        <div v-if="elform === 0">
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入账号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password" style="margin-bottom:0">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
            <div @click="forgetPassWord" style="float:right;margin:5px 0;font-size:14px;cursor:pointer">忘记密码</div>
        </div>
        <!-- 验证码登录 -->
        <div v-else>
          <el-form-item prop="phone">
            <el-input
              ref="phone"
              v-model="loginForm.telemPhone"
              placeholder="请输入手机号码"
              name="phone"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password" style="postion:relative">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.code"
              :type="passwordType"
              placeholder="请输入验证码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span v-show="show" class="code" @click="getCooded">获取验证码</span>
            <span v-show="!show" class="code">{{count}}s</span>
          </el-form-item>
        </div>

        <el-button
          :loading="loading"
          style="width: 100%;background:#1DADBA; margin-bottom: 30px;color:#fff"
          @click.native.prevent="handleLogin"
          >立即登录</el-button
        >
      </div>
       <div v-if="formItem2">
          <div class="title-container">
            <h3 class="active">忘记密码</h3>
          </div>

          <el-form-item prop="phone">
            <el-input
              ref="phone"
              v-model="loginForm.telemPhone"
              placeholder="请输入手机号码"
              name="phone"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password" style="postion:relative">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.code"
              :type="passwordType"
              placeholder="请输入验证码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span v-show="show" class="code" @click="getCooded">获取验证码</span>
            <span v-show="!show" class="code">{{count}}s</span>
          </el-form-item>

          <el-form-item prop="password" >
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
            <el-form-item prop="password" >
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="确认密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </el-form-item>
           <el-button
          :loading="loading"
          style="width: 100%;background:#1DADBA; margin-bottom: 30px;color:#fff"
          @click.native.prevent="handleLogin"
          >确认修改</el-button
        >
       </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入账号！"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("请输入密码！"));
      } else {
        callback();
      }
    };
    const validatephone = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("请输入手机号！"));
      } else {
        callback();
      }
    };
    return {
      elform:0,
      list:['账号登录','验证码登录'],
      show: true,
      count: '',
      timer: null,
      formItem1:true,
      formItem2:false,
      loginForm: {
        username: "admin",
        password: "111111",
        telemPhone:"15920719217",
        code:'333'
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        phone: [
          { required: true, trigger: "blur", validator: validatephone },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    forgetPassWord(){
      this.formItem1=false;
      this.formItem2=true;
    },
    getCooded(){
     const TIME_COUNT = 4;
     if (!this.timer) {
       this.count = TIME_COUNT;
       this.show = false;
       this.timer = setInterval(() => {
       if (this.count > 0 && this.count <= TIME_COUNT) {
         this.count--;
        } else {
         this.show = true;
         clearInterval(this.timer);
         this.timer = null;
        }
       }, 1000)
      }
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    userLoging(){
      this.elform = true;
    }
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-box {
  display: flex;
}

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .code{
    position: absolute;
    right: 0;
    top: 0;
    background: #1DADBA;
    color: #fff;
    width: 100px;
    height: 100%;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;   
    cursor: pointer;
  }
  .title-container {
    position: relative;
    display: flex;
    .title {
      font-size: 24px;
      // color: $light_gray;
      margin: 0px auto 40px auto;
      font-weight: bold;
    }
    .active {
      font-size: 30px;
      color: rgb(29, 173, 186);
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
